package com.ezia.coreservices.charging.service.impl;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ezia.coreservices.charging.dao.MaStationInfoDao;
import com.ezia.coreservices.charging.service.MaPoiService;

import net.sf.json.JSONObject;

@Service
public class MaPoiServiceImpl implements MaPoiService{

	@Autowired
	private MaStationInfoDao stationInfoDao;

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> findByPoiName(Map<String, Object> map) {
		// TODO Auto-generated method stub
		Map<String, Object> paramsDate = (Map<String, Object>)JSONObject.fromObject(map.toString());
		Map<String, Object> userInfo =(Map) paramsDate.get("userInfo");
		//paramsDate.put("userlongitude", userInfo.get(""));
		//paramsDate.put("userlatitude", userInfo.get("") );
		//paramsDate.put(key, value);
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		List<Map<String, Object>> stationinfo = new ArrayList<Map<String, Object>>();
		int pageCount = 0;
		int pageNo = 0;		
		if(null!=paramsDate.get("pageSize")&&null!=paramsDate.get("pageNo").toString()) 
		{
			int pageSize = Integer.parseInt(paramsDate.get("pageSize").toString());
			pageCount =pageSize;
			pageNo = Integer.parseInt(paramsDate.get("pageNo").toString());	
			int limit  = pageSize;
			int offset = (pageNo-1)*pageSize;
			paramsDate.put("limit", limit);
			paramsDate.put("offset", offset);
		}else 
		{
			pageCount =50;
			pageNo =1;
			int offset = 0;
			paramsDate.put("limit", pageCount);
			paramsDate.put("offset", offset);
		}
		stationinfo = stationInfoDao.queryStation(paramsDate);
		result.put("total ", stationinfo.size());
		result.put("pageCount ", pageCount);
		result.put("pageNo", pageNo);
		String stationId = null;
		if(paramsDate.get("scope").toString().equals("1")) 
		{
			
			for(int i = 0;i<stationinfo.size();i++) 
			{
				stationId = (String)stationinfo.get(i).get("stationId");
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
			}
			result.put("stationinfos", stationinfo);	
			return result;
		}
		else 
		{
			for(int i = 0;i<stationinfo.size();i++) 
			{
				stationId = (String)stationinfo.get(i).get("stationId");
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
				Map<String, Object> detailMap = new LinkedHashMap<String, Object>();
				detailMap = stationInfoDao.queryStationDetailInfo(stationId);
				List<Map<String, Object>> equipmentList = stationInfoDao.queryEquipmentInfoById(stationId);				
				for(int m =0;m<equipmentList.size();m++) 
				{
					String equipmentId = (String)equipmentList.get(m).get("EquipmentID");
					equipmentList.get(m).put("ConnectorInfos", stationInfoDao.queryConnectorInfoById(equipmentId));
				}
				detailMap.put("equplist", equipmentList);
				stationinfo.get(i).put("detail_info",detailMap);
				
				
			}
			result.put("stationinfos", stationinfo);
			return result;
		}
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> findbylocation(Map<String, Object> map) {
		// TODO Auto-generated method stub
		Map<String, Object> paramsDate = (Map<String, Object>)JSONObject.fromObject(map.toString());
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		List<Map<String, Object>> stationinfo = new ArrayList<Map<String, Object>>();
		int pageCount = 0;
		int pageNo = 0;		
		if(null!=paramsDate.get("pageSize")&&null!=paramsDate.get("pageNo").toString()) 
		{
			int pageSize = Integer.parseInt(paramsDate.get("pageSize").toString());
			pageCount =pageSize;
			pageNo = Integer.parseInt(paramsDate.get("pageNo").toString());	
			int limit  = pageSize;
			int offset = (pageNo-1)*pageSize;
			paramsDate.put("limit", limit);
			paramsDate.put("offset", offset);
		}else 
		{
			pageCount =50;
			pageNo =1;
			int offset = 0;
			paramsDate.put("limit", pageCount);
			paramsDate.put("offset", offset);
		}
		stationinfo = stationInfoDao.queryStationByLocation(paramsDate);
		result.put("total ", stationinfo.size());
		result.put("pageCount ", pageCount);
		result.put("pageNo", pageNo);
		String stationId = null;
		if(paramsDate.get("scope").toString().equals("1")) 
		{
			
			for(int i = 0;i<stationinfo.size();i++) 
			{
				stationId = (String)stationinfo.get(i).get("stationId");
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
			}
			result.put("stationinfos", stationinfo);	
			return result;
		}
		else 
		{
			for(int i = 0;i<stationinfo.size();i++) 
			{
				stationId = (String)stationinfo.get(i).get("stationId");
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
				Map<String, Object> detailMap = new LinkedHashMap<String, Object>();
				detailMap = stationInfoDao.queryStationDetailInfoByLocation(stationId);
				List<Map<String, Object>> equipmentList = stationInfoDao.queryEquipmentInfoById(stationId);				
				for(int m =0;m<equipmentList.size();m++) 
				{
					String equipmentId = (String)equipmentList.get(m).get("EquipmentID");
					equipmentList.get(m).put("ConnectorInfos", stationInfoDao.queryConnectorInfoById(equipmentId));
				}
				detailMap.put("equplist", equipmentList);
				stationinfo.get(i).put("detail_info",detailMap);
				
				
			}
			result.put("stationinfos", stationinfo);
			return result;
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getdynamicinfo(Map<String, Object> map) {
		Map<String, Object> paramsDate = (Map<String, Object>)JSONObject.fromObject(map.toString());
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		List<Map<String, Object>> stationStatusInfo =stationInfoDao.queryStationById(paramsDate.get("stationId").toString());
		for(int i =0;i<stationStatusInfo.size();i++) 
		{
			String stationId = (String)stationStatusInfo.get(i).get("StationID");			
			List<Map<String, Object>> statusList =stationInfoDao.queryConnectorStatusInfoById(stationId);
			stationStatusInfo.get(i).put("ConnectorStatusInfos", statusList);
		}		
		result.put("stationStatusInfo", stationStatusInfo);
		return result;
	}

}